package com.hmall.user.interceptor;
import com.hmall.user.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long authorization_ = Long.valueOf(request.getHeader("authorization "));
        if(StringUtils.isEmpty(authorization_.toString())){
           log.warn("非法用户访问",request.getRequestURI());
        }
        UserHolder.setUser(authorization_);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
          UserHolder.removeUser();
    }
}
